package io.finer.erp.jeecg.jolimark.print;

import io.finer.erp.jeecg.jolimark.comm.Config;
import io.finer.erp.jeecg.jolimark.enums.FileTypeEnum;
import io.finer.erp.jeecg.jolimark.enums.PrinterEnum;
import io.finer.erp.jeecg.jolimark.model.PrintFileRequestModel;
import io.finer.erp.jeecg.jolimark.model.PrintRequestModel;

/**
 * 文档打印
 * 
 * @author Jolimark
 *
 */
public class FilePrint extends AbstractPrint {

	/**
	 * 打印本地文档
	 * <p>
	 * 适用场景：适用于调用方不方便提供文档的url地址，如C/S程序对接
	 * </p>
	 * <p>
	 * 适用机型：CLP-180、CLQ-200、CLQ-200F、CFP-535、CFP-536、CFP-820
	 * </p>
	 * 
	 * @param printerEnum 打印机型号
	 * @param fileType    打印文档类型(PDF、Image)
	 * @param file        打印文档，将转换成base64格式的数据，其中PDF数据大小不超过过4M，图片数据大小不超过2M。
	 * @param paperHeight 打印纸高度
	 */
	public void localFilePrint(PrinterEnum printerEnum, FileTypeEnum fileType, String file,int paperHeight) {
		if (printerEnum.getType() == 1) {
			Out("错误：不支持MCP系列打印机");
			return;
		}
		PrintFileRequestModel model = buildFileRequest(printerEnum, fileType);
		model.setBillContent(file);
		model.setPaperHeight(paperHeight);
		print("PrintFile", model);
	}

	/**
	 * 打印远程文档URL
	 * 
	 * @param printerEnum 打印机型号
	 * @param fileType    打印文档类型(PDF、Image)
	 * @param url         打印文档URL
	 * @param paperHeight 打印纸高度
	 */
	public void fileUrlPrint(PrinterEnum printerEnum, FileTypeEnum fileType, String url, int paperHeight) {
		if (printerEnum.getType() == 1) {
			Out("错误：不支持MCP系列打印机");
			return;
		}
		PrintFileRequestModel model = buildFileRequest(printerEnum, fileType);
		model.setBillContent(url);
		model.setPaperHeight(paperHeight);
//		model.setPaperWidth(paperHeight);
//		model.setPaperWidth(225);
		print("PrintFileByUrl", model);
	}

	@Override
	protected String convert(PrintRequestModel model) {
		String baseString = super.convert(model);
		PrintFileRequestModel fileRequestModel = (PrintFileRequestModel) model;
		baseString += "&file_type=" + fileRequestModel.getFileType().getType();
		return baseString;
	}

	private PrintFileRequestModel buildFileRequest(PrinterEnum printerEnum, FileTypeEnum fileType) {
		PrintRequestModel baseModel = buildRequest(printerEnum);
		PrintFileRequestModel fileRequestModel = new PrintFileRequestModel();
		fileRequestModel.setAccessToken(Config.getAccessToken());
		fileRequestModel.setAppId(baseModel.getAppId());
		fileRequestModel.setCopies(baseModel.getCopies());
		fileRequestModel.setCusOrderId(baseModel.getCusOrderId());
		fileRequestModel.setDeviceIds(baseModel.getDeviceIds());
		fileRequestModel.setPaperType(baseModel.getPaperType());
		fileRequestModel.setPaperWidth(baseModel.getPaperWidth());
		fileRequestModel.setTex(baseModel.getTex());
		fileRequestModel.setTimeout(baseModel.getTimeout());

		fileRequestModel.setFileType(fileType);
		return fileRequestModel;
	}
}
